﻿//--------------------------------------------------------------
//
//作    者：蒙孝宗
//功能描述：数据库操作接口
//创建时间：2012-02-12
//修改记录：
//版    本：V1.0
//
//--------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace DbHelper.Data
{
    /// <summary>
    /// 数据库操作接口
    /// </summary>
    public interface IProvider : IDisposable
    {
        /// <summary>
        /// 连接数据库的对象
        /// </summary>
        IDbConnection DbConnection { get; }
        /// <summary>
        /// 执行 SQL 语句或存储过程的对象
        /// </summary>
        IDbCommand DbCommand { get; }
        /// <summary>
        /// 数据库适配器
        /// </summary>
        IDbDataAdapter DbDataAdapter { get; }

        /// <summary>
        /// 打开数据库
        /// </summary>
        void Open();
        /// <summary>
        /// 关闭数据库
        /// </summary>
        void Close();

        /// <summary>
        /// 执行 SQL 语句
        /// </summary>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns></returns>
        int Execute(string cmdText);
        /// <summary>
        /// 执行 SQL 语句或存储过程
        /// </summary>
        /// <param name="cmdType">SQL 类型</param>
        /// <param name="cmdText">SQL 语句或存储过程</param>
        /// <param name="paraValues">参数</param>
        /// <returns></returns>
        int Execute(CommandType cmdType, string cmdText, params IDataParameter[] paraValues);

        /// <summary>
        /// 执行 SQL 语句
        /// </summary>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns></returns>
        object ExecuteScalar(string cmdText);
        /// <summary>
        /// 执行 SQL 语句或存储过程
        /// </summary>
        /// <param name="cmdType">SQL 类型</param>
        /// <param name="cmdText"> SQL 语句或存储过程</param>
        /// <param name="paraValues">参数</param>
        /// <returns></returns>
        object ExecuteScalar(CommandType cmdType, string cmdText, params IDataParameter[] paraValues);

        /// <summary>
        /// 读取数据
        /// <para>注意：一定要手动关闭 DbDataReader</para>
        /// </summary>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns></returns>
        IDataReader ExecuteReader(string cmdText);
        /// <summary>
        /// 读取数据
        /// <para>注意：一定要手动关闭 DbDataReader</para>
        /// </summary>
        /// <param name="cmdType">SQL 类型</param>
        /// <param name="cmdText">SQL 语句或存储过程</param>
        /// <param name="paraValues">参数</param>
        /// <returns></returns>
        IDataReader ExecuteReader(CommandType cmdType, string cmdText, params IDataParameter[] paraValues);

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="cmdText">SQL 语句</param>
        /// <returns></returns>
        DataTable FetchData(string cmdText);
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="cmdType">SQL 类型</param>
        /// <param name="cmdText">SQL 语句或存储过程</param>
        /// <param name="paraValues">参数</param>
        /// <returns></returns>
        DataTable FetchData(CommandType cmdType, string cmdText, params IDataParameter[] paraValues);
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="cmdTexts">SQL 语句</param>
        /// <returns></returns>
        DataSet FetchData(string[] cmdTexts);
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="cmdType">SQL 类型</param>
        /// <param name="dictSQL">SQL 语句或存储过程及其参数</param>
        /// <returns></returns>
        DataSet FetchData(CommandType cmdType, Dictionary<string, IDataParameter[]> dictSQL);

        /// <summary>
        /// 创建参数对象
        /// </summary>
        /// <returns></returns>
        IDataParameter CreateParameter(string name, object value);

        /// <summary>
        /// 获取表结构信息
        /// </summary>
        /// <param name="tableName">数据库中的表名</param>
        /// <returns></returns>
        DataTable GetTableSchema(string tableName);
    }
}
